package com.clusterswisdom.horn.api.tmall.action.bill;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.bill.data.BillFee;
import com.clusterswisdom.horn.api.tmall.action.bill.data.ChannelBill;
import com.clusterswisdom.horn.api.tmall.action.bill.data.ChannelOrderBillItem;
import com.taobao.api.domain.Bill;
import com.taobao.api.response.BillBillsGetResponse;
import com.taobao.api.response.TradeFullinfoGetResponse;

public class SendGenAccountsAction extends SendOrderAccountsAction implements HornAction {

//	public HornResponse execute(ApiConfig config, JSONObject param) throws Exception {
//		ChannelBill bill = new ChannelBill();
//		bill.setChannel(CHANNEL_NAME);
//		bill.setBillDateStr(getBillDateStr(param));
//		bill.setBillSpanStr(getBillSpanStr(param));
//		
//		return new HornResponse(true, formateBill(bill, param));
//	}
//	
//	/**
//	 * 汇总所有   billNum  totalMoney  busSalesAmount busRefundAmount  busShippingAmount  
//	 * countMoney feeList  feeCode feeName feeValue  differFee
//	 * 
//	 * @param bill
//	 * @param param
//	 * @return
//	 * @throws Exception
//	 */
//	private ChannelBill formateBill(ChannelBill bill, JSONObject param) throws Exception {
//		List<ChannelOrderBillItem> orderBillItems = super.getDaysOrderBills(param);
//		//TODO 
//		double totalMoney = 0.00;
//		double busSalesAmount = 0.00;
//		double busShippingAmount = 0.00;
//		double countMoney = 0.00;
//		List<BillFee> feeList = new LinkedList<BillFee>();
//		String feeCode = null;
//		String feeName = null;
//		String feeValue = null;
//		double differFee = 0.00;
//		
//		bill.setBillNum(String.valueOf(orderBillItems.size()));//billNum （订单数量）
//		bill.setBusRefundAmount(getBusRefundAmount(param));//busRefundAmount（商家退款额）
//		for(int i = 0; i<orderBillItems.size(); i++) {
//			String torderId = orderBillItems.get(i).getTorderid();//订单编号
//			double t_money = Double.parseDouble(orderBillItems.get(i).getTotalMoney());
//			double salesAmount = getBusSalesAmount(torderId,param);
//			double post_fee = getPostFee(torderId,param);
//            double c_money = Double.parseDouble(orderBillItems.get(i).getCountMoney());
//            List<BillFee> list = orderBillItems.get(i).getFeeList();
//            if(list != null) {
//            	feeList.addAll(list);
//            }
//            double differ_Fee = Double.parseDouble(orderBillItems.get(i).getDifferFee());
//			
//			totalMoney += t_money;
//			busSalesAmount =+ salesAmount;
//			busShippingAmount += post_fee;
//			countMoney += c_money;
//			differFee += differ_Fee;
//		}
//		bill.setTotalMoney(String.valueOf(totalMoney));//totalMoney（当期发生款项）
//		bill.setBusSalesAmount(String.valueOf(busSalesAmount));//busSalesAmount（销售额）
//		bill.setBusShippingAmount(String.valueOf(busShippingAmount));//busShippingAmount（运费）
//		bill.setCountMoney(String.valueOf(countMoney));//countMoney（当期结算金额）
//		bill.setFeeList(getFeeList(feeList));//
//		bill.setDifferFee(String.valueOf(differFee));// differFee（差额）
//		return bill;
//	}
//	
//	private double getBusSalesAmount(String torderId,JSONObject param) throws Exception {
//		param.put("torderId", torderId);
//		TradeFullinfoGetResponse trade_response = (TradeFullinfoGetResponse) TmallInstance
//				.getInstance().executeRequest(param, "trade",
//						"tradeFullinfoGet");
//		double salesAmount = Double.parseDouble(trade_response.getTrade().getTotalFee());
//		return salesAmount;
//	}
//	
//	private String getBusRefundAmount(JSONObject param) throws Exception {
//		Long refundAmount = 0l;
//		BillBillsGetResponse response = (BillBillsGetResponse) TmallInstance
//				.getInstance().executeRequest(param, "bill", "billBillsGet");
//		List<Bill> bill_list = response.getBills();
//		for(int i = 0;i<bill_list.size();i++) {
//			Long bill_amount = bill_list.get(i).getAmount();
//			refundAmount += bill_amount;
//		}
//		return refundAmount.toString();
//		
//	}
//	
//	private double getPostFee(String torderId,JSONObject param) throws Exception {
//		param.put("torderId", torderId);
//		TradeFullinfoGetResponse trade_response = (TradeFullinfoGetResponse) TmallInstance
//				.getInstance().executeRequest(param, "trade",
//						"tradeFullinfoGet");
//		double postFee = Double.parseDouble(trade_response.getTrade().getPostFee());
//		return postFee;
//	}
//	
//	private List<BillFee> getFeeList(List<BillFee> feeList) {
//		List<BillFee> list = new ArrayList<BillFee>();
//		int [] num = new int [feeList.size()];
//		for(int i=0;i<feeList.size();) {
//			BillFee bill_fee = new BillFee();
//			String feeCode = feeList.get(i).getFeeCode();
//			String feeName = feeList.get(i).getFeeName();
//			String feeValue = feeList.get(i).getFeeValue();
//			num[i]=i;
//			for(int j=i+1;j<feeList.size();j++) {
//				if(feeCode.equals(feeList.get(j).getFeeCode())) {
//					num[j]=j;
//					double fee_Value = Double.parseDouble(feeList.get(j).getFeeValue());
//					feeValue = String.valueOf(Double.parseDouble(feeValue)+fee_Value);
//				} 
//			}
//			bill_fee.setFeeCode(feeCode);
//			bill_fee.setFeeName(feeName);
//			bill_fee.setFeeValue(feeValue);
//			list.add(bill_fee);
//			
//			i = i+1;
//		    for(int index:num) {
//		    	if(i==(index)){
//		    		i++;
//		    	}
//		    }
//		}
//		
//		return list;
//	}
//	
	@Override
	public HornResponse execute(ApiConfig config, JSONObject param)
			throws Exception {
		// TODO Auto-generated method stub
		return super.execute(config, param);
	}
}
